/**
 * Code Sample for
 * Runtime Performance & Availability Monitoring for Java Systems.
 * IBM DevelperWorks.
 * Nicholas Whitehead (whitehead.nicholas@gmail.com)
 */

package org.digitarts.rtms.spring.collectors.management;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import org.digitarts.rtms.spring.collectors.CollectorBase;
import org.springframework.jmx.export.naming.ObjectNamingStrategy;


public class DelegatingNamingStrategy implements ObjectNamingStrategy {

	/**
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws MalformedObjectNameException
	 */
	public ObjectName getObjectName(Object object, String name)
			throws MalformedObjectNameException {
		if(object instanceof CollectorBase) {
			try {
				return (ObjectName)object.getClass().getMethod("getObjectName", new Class[]{}).invoke(object, new Object[]{});
			} catch (Exception e) {
				throw new RuntimeException("Failed to acquire ObjectName", e);
			} 	
		} else {
			throw new RuntimeException("Not an instance of CollectorBase");
		}
	}

}
